//
//  Date_y.swift
//  Library_y
//
//  Created by JC on 2023/6/6.
//

import Foundation

// MARK: - 日期相关工具
extension Tools {
    
    /// 当前毫秒时间戳
    static public var stamp: Int {
        return Int(Date().timeIntervalSince1970 * 1000)
    }
     
     // 根据日期获取当天零点的时间
     static public func getMorningDate(_ date: Date) -> Date? {
          guard let calendar = NSCalendar(identifier: .chinese) else { return nil }
          let components = calendar.components([.year,.month,.day], from: date)
          return (calendar.date(from: components))
     }
     
    // 根据日期获取当天零点的时间戳
    static public func getMorningStamp(_ date: Date) -> Int? {
        guard let calendar = NSCalendar(identifier: .chinese) else { return nil }
        let components = calendar.components([.year,.month,.day], from: date)
        guard let a = calendar.date(from: components) else { return nil }
        let dfmatter = DateFormatter()
        dfmatter.dateFormat = JCDateFormatEnum._YMDHMS.format
        return Tools.stringToTimeStamp(dfmatter.string(from: a as Date), type: ._YMDHMS)
     }
    
    /// 字符串转时间类型
    ///
    /// - Returns: 时间类型
    static public func stringToDate(_ YDM: String, type: JCDateFormatEnum) -> Date {
        let dfmatter = DateFormatter()
        dfmatter.dateFormat = type.format
        guard let date = dfmatter.date(from: YDM) else { return Date() }
        return date
    }
    
    /// 字符串转时间戳
    ///
    /// - Parameter stringTime: 时间字符串
    /// - Returns: 时间戳
    static public func stringToTimeStamp(_ str: String, type: JCDateFormatEnum) -> Int {
        let dfmatter = DateFormatter()
        dfmatter.dateFormat = type.format
        guard let date = dfmatter.date(from: str)  else { return 0 }
        let dateStamp: TimeInterval = date.timeIntervalSince1970
        return Int(dateStamp)
        
    }
    
    /// 时间戳转换为年月日
    ///
    /// - Parameter timeStamp: 时间戳
    /// - Returns: 年月日
    static public func stampToDateStr(_ stamp: Int, type: JCDateFormatEnum) -> String {
        let string = NSString(format: "%d", stamp)
        let timeSta: TimeInterval = string.doubleValue
        let dfmatter = DateFormatter()
        dfmatter.dateFormat = type.format
        let date = NSDate(timeIntervalSince1970: timeSta)
        return dfmatter.string(from: date as Date)
    }
    
    /// 获取前几天的时间
    ///
    /// - Returns: 时间
    static public func lastDay(_ lastDayCount: Int = 1) -> Date {
        let lastDay = Date(timeInterval: TimeInterval(-24 * 60 * 60 * lastDayCount), since: Date())
        return lastDay
    }
    
    /// Calendar的isDate方法进行判断（推荐）
    static public func isSameDay(_ date1: Date, _ date2: Date) -> Bool {
        return Calendar.current.isDate(date1, inSameDayAs: date2)
    }
     
     /// 获取制定时间的，上一个月，下一个月
     static public func getLastMonth(num : Int) -> String? {
          let curDate = Date()
          let formater = DateFormatter()
          formater.dateFormat = "yyyy年MM月dd日"
          let calendar = Calendar(identifier: .gregorian)
          var lastMonthComps = DateComponents()
          // year = 1表示1年后的时间 year = -1为1年前的日期，month day 类推
          lastMonthComps.month = num
          guard let newDate = calendar.date(byAdding: lastMonthComps, to: curDate) else { return nil }
          let dateStr = formater.string(from: newDate)
          return dateStr
     }
}
